package org.wannatrak.android.helper;

import org.wannatrak.android.ApplicationContext;
import org.wannatrak.android.formatter.TimeFormatter;
import org.wannatrak.android.intf.Formatter;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: Maksim Tulupov maxtulupov@gmail.com
 * @Date: $Date: 2011-07-07 05:24:32 +0000 (Thu, 07 Jul 2011) $ 5:55 PM
 * @version: $Revision: 25 $
 * @id: $Id: DateHelper.java 25 2011-07-07 05:24:32Z maxtulupov@gmail.com $
 */
public class DateHelper {

    public static Date getDateMidnight(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.HOUR_OF_DAY, 0);

        return c.getTime();
    }

    public static Date getDateMidnight() {
        return getDateMidnight(ApplicationContext.getCommonService().getCurrentDate());
    }

    public static Date getDate(int year, int month, int day, int hour, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getDate(int year, int month, int day) {
        return getDate(year, month, day, 0, 0);
    }

    public static Date createDateMidnight(Date base) {
        Calendar c = Calendar.getInstance();
        c.setTime(base);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    public static Date getDate(long value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(value);
        return calendar.getTime();
    }

    public static Date getDate(String date, String time) throws ParseException {
        Calendar calendar = Calendar.getInstance();

        Formatter dateFormatter = ApplicationContext.getFormatter(Date.class);
        calendar.setTime((Date) dateFormatter.parse(date));

        Formatter timeFormatter = ApplicationContext.getFormatter(TimeFormatter.class);
        Date d = (Date) timeFormatter.parse(time);
        calendar.set(Calendar.HOUR, d.getHours());
        calendar.set(Calendar.MINUTE, d.getMinutes());

        return calendar.getTime();
    }

    public static Date getFirstDayOfCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDateMidnight());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        return calendar.getTime();
    }

    public static Date getCurrentDateTime() {
        return Calendar.getInstance().getTime();
    }
}
